package Store;

import java.util.Vector;

import Auxiliary.Sequence_Event;
import Auxiliary.Sequence_SplitInfo;
import DataView.EventInfo;
import DataView.Sequence_Error;
import Visualization.EventCondReactVis;
import Visualization.EventVis;

public class EventCondReact extends Element 
{
	public Sequence_Event events = new Sequence_Event();

	public Sequence_Error check(StateObserverInv s)
	{
		Sequence_Error lista = new Sequence_Error();
		
		for (Event e : events.getAll())
		{	
			int i=0;
			for (Event z : events.getAll())
				if(e.getName().equals(z.getName()))
					i++;
			if(i>1)
			{
				lista.add("There are  Two or more Events with the name "+e.getName());
				break;
			}
		}
		for (Event e: events.getAll())
		{
			for(Parameter p : e.getInfo().getParameters())
			{
				int i=0;
				for(Parameter y : e.getInfo().getParameters())
					if(p.getName().equals(y.getName()))
						i++;
				if(i>1){
					lista.add("In "+e.getName()+" There are Two or more Parameters with the name "+ p.getName());
					break;
				}
				}
		}
		for (Event e : events.getAll())
		{
			int i=0;
			for (StateObserver w : s.list_stateObserver.getAll())
				if (e.getName().equals(w.getName()))
					i++;
			if(i>0){
				lista.add("There are an Event and a StateObserver with the same name "+e.getName());
				break;
			}
			}
		
		
		
		
		return lista;
	}
	
	public void addEvent(Event aSo) 
	{
		events.add(aSo);
	}

	public void addEvents(Sequence_Event aSo) 
	{
		for(Event e : aSo.getAll())
			addEvent(e);
	}


	public void deleteEvent(Event aSo) 
	{
		events.delete(aSo);
	}

	public void updateEvent(Event aSo, EventInfo aNewEv) 
	{
		int max=events.size();
		for(int i=0; i<max; ++i)
			if(events.get(i).equals(aSo))
				{
				events.get(i).setInfo(aNewEv);
				System.out.println("modifica Eseguita");
				}
	}

	public void split(Sequence_SplitInfo split) 
	{
		int i=0;
		for(Event e : events.getAll())
			for(CondReact cr : e.getInfo().getCondReact().getAll())
				cr.setSplit(split.get(i).getCondUsed() , split.get(i).getCondRest(), split.get(i).getReactUsed(), split.get(i++).getReactRest());
		
	}
	
	public Event trova(String name)
	{
		for(Event e :events.getAll())
			if(e.getName()==name)
				return e;
		return null;
	}
	
	public Event trova_from_desc(String desc)
	{
		for(Event e :events.getAll())
			if(desc.compareTo(e.getDescription())==0)
				return e;
		return null;
	}

	public EventCondReactVis vis() 
	{
		Vector<EventVis> lista_vis = new Vector<EventVis>();
		for (Event e: events.getAll())
			lista_vis.add(e.vis());
		return new EventCondReactVis(lista_vis); 
	}

	public Event get(int selectedRow) 
	{
		return events.get(selectedRow);
	}
}